package org.answer.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.answer.dao.PermissionDao;
import org.answer.dao.RoleDao;
import org.answer.dao.UserDao;
import org.answer.entity.PageResult;
import org.answer.entity.QueryPageBean;
import org.answer.pojo.CheckItem;
import org.answer.pojo.Permission;
import org.answer.pojo.Role;
import org.answer.pojo.User;
import org.answer.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;


    /**
     * 1. 根据用户名查询所有
     * 2. 根据 用户（user）id查询对应的角色（role）
     * 3. 根据角色（role）id 查询对应权限（permission）
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        if(roles != null && roles.size()>0){
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if(permissions != null && permissions.size()>0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //设置分页查询,基于mybatis框架
        PageHelper.startPage(currentPage,pageSize);

        //select * from t_checkitem limit 0,10
        Page<User> page = userDao.selectByCondition(queryString);

        long total = page.getTotal();

        List<User> rows = page.getResult();

        for (User row : rows) {
            if (row.getBirthday()!=null){
                Date birthday = row.getBirthday();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String format = sdf.format(birthday);
                row.setSdfbirthday(format);
            }
        }

        return new PageResult(total,rows);
    }

    @Override
    public void add(User user, Integer[] checkRoleIds) {
        userDao.add(user);
        Integer id = user.getId();
        insert(id,checkRoleIds);
    }


    @Override
    public List<Integer> findById(Integer id) {
        List<Integer> roleids = userDao.findById(id);
        return roleids;
    }

    @Override
    public void delete(Integer id) {
        userDao.delete(id);
    }

    @Override
    public void AllDelete(Integer id) {


        List<Integer> roleids = userDao.findById(id);//检查关联关联表是否有数据


        if (roleids.size()>0){
            userDao.delete(id);  //删除关联表
        }
        userDao.AllDelete(id); //啥拿出数据

    }

    @Override
    public void insert(Integer id, Integer[] checkRoleIds) {
        for (Integer checkRoleId : checkRoleIds) {
            userDao.insert(id,checkRoleId);
        }
    }

    @Override
    public User findUserByid(Integer id) {

        User userByid = userDao.findUserByid(id);
        return userByid;
    }

    @Override
    public void edit(User user) {
        userDao.edit(user);
    }


}
